package com.sh.atalk;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.alibaba.mobileim.IYWLoginService;
import com.alibaba.mobileim.YWAPI;
import com.alibaba.mobileim.YWIMKit;
import com.alibaba.mobileim.YWLoginParam;
import com.alibaba.mobileim.channel.event.IWxCallback;
import com.alibaba.mobileim.conversation.EServiceContact;

public class MainActivity extends AppCompatActivity {

	private Button mButton;
	private YWIMKit mIMKit;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		//此实现不一定要放在Application onCreate中
		final String userid = "testpro1";
		final String APP_KEY = "23015524";
		//此对象获取到后，保存为全局对象，供APP使用
		//此对象跟用户相关，如果切换了用户，需要重新获取
		mIMKit = YWAPI.getIMKitInstance(userid, APP_KEY);

		mButton = (Button)findViewById(R.id.btn_login);
		mButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				//开始登录
				String userid = "testpro1";
				String password = "taobao1234";
				IYWLoginService loginService = mIMKit.getLoginService();
				YWLoginParam loginParam = YWLoginParam.createLoginParam(userid, password);
				loginService.login(loginParam, new IWxCallback() {

					@Override
					public void onSuccess(Object... arg0) {
						//userid是客服帐号，第一个参数是客服帐号，第二个是组ID，如果没有，传0
						EServiceContact contact = new EServiceContact("testpro2", 0);
						//如果需要发给指定的客服帐号，不需要Server进行分流(默认Server会分流)，请调用EServiceContact对象
						//的setNeedByPass方法，参数为false。
						//contact.setNeedByPass(false);
						Intent intent = mIMKit.getChattingActivityIntent(contact);
						startActivity(intent);
					}

					@Override
					public void onProgress(int arg0) {
						// TODO Auto-generated method stub
					}

					@Override
					public void onError(int errCode, String description) {
						//如果登录失败，errCode为错误码,description是错误的具体描述信息
					}
				});
			}
		});
	}
}
